﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace DMZS.DAL
{
    /// <summary>
    /// 数据访问类:Vchtype
    /// </summary>
    public partial class Vchtype
    {
        public Vchtype()
        {
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("vchtype", "Vchtype");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int vchtype)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from Vchtype");
            strSql.Append(" where vchtype=@vchtype");
            var parameters = new SqlParameter[] { new SqlParameter("@vchtype", SqlDbType.Int, 4)
            };
            parameters[0].Value = vchtype;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DMZS.Model.Vchtype model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into Vchtype(");
            strSql.Append("vname,vfullname,comment,deleted,numberhead,SNFormat,SNMax,StringNo,vCode,isSystem)");
            strSql.Append(" values (");
            strSql.Append("@vname,@vfullname,@comment,@deleted,@numberhead,@SNFormat,@SNMax,@StringNo,@vCode,@isSystem)");
            strSql.Append(";select @@IDENTITY");
            var parameters = new SqlParameter[] { new SqlParameter("@vname", SqlDbType.VarChar, 30),
                    new SqlParameter("@vfullname", SqlDbType.VarChar, 100),
                    new SqlParameter("@comment", SqlDbType.VarChar, 254),
                    new SqlParameter("@deleted", SqlDbType.Bit, 1),
                    new SqlParameter("@numberhead", SqlDbType.VarChar, 10),
                    new SqlParameter("@SNFormat", SqlDbType.VarChar, 100),
                    new SqlParameter("@SNMax", SqlDbType.Int, 4),
                    new SqlParameter("@StringNo", SqlDbType.Int, 4),
                    new SqlParameter("@vCode", SqlDbType.VarChar, 30),
                    new SqlParameter("@isSystem", SqlDbType.Int, 4) };
            parameters[0].Value = model.vname;
            parameters[1].Value = model.vfullname;
            parameters[2].Value = model.comment;
            parameters[3].Value = model.deleted;
            parameters[4].Value = model.numberhead;
            parameters[5].Value = model.SNFormat;
            parameters[6].Value = model.SNMax;
            parameters[7].Value = model.StringNo;
            parameters[8].Value = model.vCode;
            parameters[9].Value = model.isSystem;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DMZS.Model.Vchtype model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update Vchtype set ");
            strSql.Append("vname=@vname,");
            strSql.Append("vfullname=@vfullname,");
            strSql.Append("comment=@comment,");
            strSql.Append("deleted=@deleted,");
            strSql.Append("numberhead=@numberhead,");
            strSql.Append("SNFormat=@SNFormat,");
            strSql.Append("SNMax=@SNMax,");
            strSql.Append("StringNo=@StringNo,");
            strSql.Append("vCode=@vCode,");
            strSql.Append("isSystem=@isSystem");
            strSql.Append(" where vchtype=@vchtype");
            var parameters = new SqlParameter[] { new SqlParameter("@vname", SqlDbType.VarChar, 30),
                    new SqlParameter("@vfullname", SqlDbType.VarChar, 100),
                    new SqlParameter("@comment", SqlDbType.VarChar, 254),
                    new SqlParameter("@deleted", SqlDbType.Bit, 1),
                    new SqlParameter("@numberhead", SqlDbType.VarChar, 10),
                    new SqlParameter("@SNFormat", SqlDbType.VarChar, 100),
                    new SqlParameter("@SNMax", SqlDbType.Int, 4),
                    new SqlParameter("@StringNo", SqlDbType.Int, 4),
                    new SqlParameter("@vCode", SqlDbType.VarChar, 30),
                    new SqlParameter("@isSystem", SqlDbType.Int, 4),
                    new SqlParameter("@vchtype", SqlDbType.Int, 4) };
            parameters[0].Value = model.vname;
            parameters[1].Value = model.vfullname;
            parameters[2].Value = model.comment;
            parameters[3].Value = model.deleted;
            parameters[4].Value = model.numberhead;
            parameters[5].Value = model.SNFormat;
            parameters[6].Value = model.SNMax;
            parameters[7].Value = model.StringNo;
            parameters[8].Value = model.vCode;
            parameters[9].Value = model.isSystem;
            parameters[10].Value = model.vchtype;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int vchtype)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from Vchtype ");
            strSql.Append(" where vchtype=@vchtype");
            var parameters = new SqlParameter[] { new SqlParameter("@vchtype", SqlDbType.Int, 4)
            };
            parameters[0].Value = vchtype;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string vchtypelist )
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from Vchtype ");
            strSql.Append(" where vchtype in (" + vchtypelist + ")  ");
            var rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.Vchtype GetModel(int vchtype)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 vchtype,vname,vfullname,comment,deleted,numberhead,SNFormat,SNMax,StringNo,vCode,isSystem from Vchtype ");
            strSql.Append(" where vchtype=@vchtype");
            var parameters = new SqlParameter[] { new SqlParameter("@vchtype", SqlDbType.Int, 4)
            };
            parameters[0].Value = vchtype;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.Vchtype DataRowToModel(DataRow row)
        {
            var model = new DMZS.Model.Vchtype();
            if (row != null)
            {
                if (row["vchtype"] != null && row["vchtype"].ToString() != string.Empty)
                {
                    model.vchtype = int.Parse(row["vchtype"].ToString());
                }
                if (row["vname"] != null)
                {
                    model.vname = row["vname"].ToString();
                }
                if (row["vfullname"] != null)
                {
                    model.vfullname = row["vfullname"].ToString();
                }
                if (row["comment"] != null)
                {
                    model.comment = row["comment"].ToString();
                }
                if (row["deleted"] != null && row["deleted"].ToString() != string.Empty)
                {
                    if ((row["deleted"].ToString() == "1") || (row["deleted"].ToString().ToLower() == "true"))
                    {
                        model.deleted = true;
                    }
                    else
                    {
                        model.deleted = false;
                    }
                }
                if (row["numberhead"] != null)
                {
                    model.numberhead = row["numberhead"].ToString();
                }
                if (row["SNFormat"] != null)
                {
                    model.SNFormat = row["SNFormat"].ToString();
                }
                if (row["SNMax"] != null && row["SNMax"].ToString() != string.Empty)
                {
                    model.SNMax = int.Parse(row["SNMax"].ToString());
                }
                if (row["StringNo"] != null && row["StringNo"].ToString() != string.Empty)
                {
                    model.StringNo = int.Parse(row["StringNo"].ToString());
                }
                if (row["vCode"] != null)
                {
                    model.vCode = row["vCode"].ToString();
                }
                if (row["isSystem"] != null && row["isSystem"].ToString() != string.Empty)
                {
                    model.isSystem = int.Parse(row["isSystem"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select vchtype,vname,vfullname,comment,deleted,numberhead,SNFormat,SNMax,StringNo,vCode,isSystem ");
            strSql.Append(" FROM Vchtype ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" vchtype,vname,vfullname,comment,deleted,numberhead,SNFormat,SNMax,StringNo,vCode,isSystem ");
            strSql.Append(" FROM Vchtype ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Vchtype ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            var obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby );
            }
            else
            {
                strSql.Append("order by T.vchtype desc");
            }
            strSql.Append(")AS Row, T.*  from Vchtype T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取类型id
        /// </summary>
        /// <param name="orderType"></param>
        /// <returns></returns>
        public int GetOrderTypeID(DMZS.Model.DlyndxOrder.OrderType orderType)
        {
            var vCode = string.Empty;
            switch (orderType)
            {
                case Model.DlyndxOrder.OrderType.CGDD:
                    vCode = "CGDD";
                    break;
                case Model.DlyndxOrder.OrderType.CPRK:
                    vCode = "CPRK";
                    break;
                case Model.DlyndxOrder.OrderType.FHD:
                    vCode = "FHD";
                    break;
                case Model.DlyndxOrder.OrderType.XOUT:
                    vCode = "XOUT";
                    break;
                case Model.DlyndxOrder.OrderType.XSDD:
                    vCode = "XSDD";
                    break;
                case Model.DlyndxOrder.OrderType.RKD:
                    vCode = "RKD";
                    break;
                case Model.DlyndxOrder.OrderType.CKD:
                    vCode = "CKD";
                    break;
            }

            var strSql = @"select vchtype from Vchtype where vCode=@vCode";
            var parameters = new SqlParameter[] { new SqlParameter(@"vCode", SqlDbType.NVarChar, 30)
            };
            parameters[0].Value = vCode;

            var obj = DbHelperSQL.GetSingle(strSql, parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
    }
}

